package blog.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import blog.entity.User;
import blog.service.UserService;
import blog.util.MD5Util;

@Controller
public class UserController {

	//依赖注入User服务类
	@Resource	
	private UserService userService;

	@RequestMapping(value = "/login")
	public String Login() {
		return "admin/login";
	}

	@RequestMapping(value = "/register")
	public String Register() {
		return "blog/register";
	}

	/**
	 * 保存用户注册信息
	 * @param user
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/doRegister", method = RequestMethod.POST)
	public ModelAndView doRegister(User user, HttpServletRequest req) {
		ModelAndView view = new ModelAndView();	
		String MD5pwd = MD5Util.MD5Encode(user.getPassword(), "UTF-8");
		user.setPassword(MD5pwd);
		if(userService.findUserByName(user.getUsername()) == null){
			userService.saveUser(user);
			view.addObject("message","博客注册成功！请登陆");		
			view.setViewName("redirect:login");
		} else {
			view.addObject("errorMsg","用户名已经存在，请输入其它的名字！");
			view.setViewName("forward:register");
		}
		return view;
	}

	/**
	 * 登录验证
	 * @param user
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/doLogin", method = RequestMethod.POST)
	public String doLogin(User user, HttpServletRequest req) {
		
		String MD5pwd = MD5Util.MD5Encode(user.getPassword(), "UTF-8");
        user.setPassword(MD5pwd);      
		HttpSession session = req.getSession();
		User existUser = userService.login(user);
		if (existUser != null) {
			String users=existUser.getUsername();			
			session.setAttribute("username", users);
			session.setAttribute("users", existUser);
			System.out.println("博客主页登录成功");
			return "admin/background";
		}
		return "redirect:login";
	}

	/**
	 * 退出登录
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/SignOut")
	public String SignOut(HttpServletRequest req) {
		HttpSession session = req.getSession(false);
		session.removeAttribute("users");
		session.removeAttribute("username");
		return "redirect:login";
	}

	/**
	 * 修改个人联系信息
	 * @return
	 */
	@RequestMapping(value = "/admin/doModify")
	public String modify(HttpServletRequest req, ModelMap map) {
		User loginUser = (User) req.getSession().getAttribute("users");
		User user = userService.findUser(loginUser.getId());
		map.put("user", user);
		return "admin/dataModify";
	}

	/**
	 * 修改普通资料
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/updateUser")
	public String updateUserdata(User user) {
		User u = userService.findUser(user.getId());
		u.setEmail(user.getEmail());
		u.setPhone(user.getPhone());
		u.setQq(user.getQq());
		u.setInfo(user.getInfo());
		userService.updateUser(u);
		return "admin/dataModify";
	}

	/**
	 * 查询个人联系信息
	 * @return
	 */
	@RequestMapping(value = "/doUser/{role}/{path}")
	public String doUser(@PathVariable String role, @PathVariable String path,HttpServletRequest req,
			ModelMap map) {
		User loginUser = (User) req.getSession().getAttribute("users");
		User user = userService.findUser(loginUser.getId());
		userService.updateUserPassword(user);
		map.put("user", user);
		System.out.println("查询个人信息成功");
		return role + "/" + path;
	}

	/**
	 * 修改账号密码
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/updateUserPassword")
	public String updateUserP(User user,HttpServletRequest req) {
		String message="";
		String oldPwd=req.getParameter("oldPwd");
		String oldMD5pwd = MD5Util.MD5Encode(oldPwd, "UTF-8");
		
		user.setPassword(oldMD5pwd); 
		User existUser = userService.login(user);
		
		if (existUser!=null) {
			String MD5pwd = MD5Util.MD5Encode(req.getParameter("password"), "UTF-8");
			user.setPassword(MD5pwd); 
			userService.updateUserPassword(user);
			message="密码更新成功，请重新登陆";	
			req.setAttribute("message", message);
			return "admin/login";
		}
		else{
			message="密码错误";
			req.setAttribute("message", message);
			return "admin/userModify";
		}
	}

	/**
	 * 页面跳转前台
	 * @return
	 */
	@RequestMapping(value = "/doHome")
	public String doHome() {
		return "blog/home";
	}

	@RequestMapping(value = "/doArticle")
	public String doArticle() {
		System.out.println("article登录成功");
		return "blog/article";
	}
	@RequestMapping(value = "/doContact")
	public String doContact() {
		return "blog/contact";
	}
}
